<template>
  <div>
    <van-list
      class="goods-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      offset="0"
    >
      <div class="goods-list-item" v-for="(good, index) in goods" :key="index">
        <div class="imgs-box">
          <img v-lazy="good.img_url" alt="" />
        </div>
        <h2 class="title">{{ good.title }}</h2>
        <div class="info">
          <div class="price">
            <span class="new">现价:&yen;{{ good.sell_price }}</span>
            <span class="old">原价:&yen;{{ good.market_price }}</span>
          </div>
          <div class="sell">
            <div class="hot">热卖中</div>
            <div class="count">剩余{{ good.stock_quantity }}件</div>
          </div>
        </div>
      </div>
    </van-list>
  </div>
</template>
<script>
import { getGoods } from '@/api/goods'
export default {
  data: () => ({
    pageNo: 0,
    pageSize: 5,
    goods: [],
    loading: false,
    finished: false,
    count: 0
  }),
  methods: {
    async getGoods() {
      ++this.pageNo
      const res = await getGoods({ pageNo: this.pageNo, pageSize: this.pageSize })
      console.log(res)
      this.goods = this.goods.concat(res.data.message)
      this.count = res.data.count
    },
    async onLoad() {
      await this.getGoods()
      this.loading = false
      if (this.goods.length >= this.count) {
        this.finished = true
      }
    }
  },
  created() {
    // this.getGoods()
  }
}
</script>
<style lang="less">
.goods-list {
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .goods-list-item {
    width: 46%;
    border: 1px solid #ddd;
    padding: 4px;
    margin-top: 10px;
    font-size: 12px;
    .imgs-box {
      width: 163px;
      height: 163px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .title {
      font-size: 14px;
    }
    .info {
      .price {
        display: flex;
        justify-content: space-between;
        margin: 4px 0;
        .new {
          color: red;
        }
        .old {
          text-decoration: line-through;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
      }
    }
  }
  .van-list_finished-text,
  .van-list_loading {
    width: 100%;
    text-align: center;
  }
}
</style>